package com.example.onlinemusicserver2024.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.constant.Constants;
import com.example.onlinemusicserver2024.mapper.PayItemMapper;
import com.example.onlinemusicserver2024.mapper.PayMapper;
import com.example.onlinemusicserver2024.mapper.SongMapper;
import com.example.onlinemusicserver2024.model.domain.Pay;
import com.example.onlinemusicserver2024.model.domain.PayItem;
import com.example.onlinemusicserver2024.model.domain.Song;
import com.example.onlinemusicserver2024.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {

    @Autowired
    SongMapper songMapper;

    @Autowired
    private PayMapper payMapper;

    @Autowired
    private PayItemMapper payItemMapper;

    @Override
    public R getSongDetail(int id) {
        // 设置查询条件
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<Song> songs = songMapper.selectList(queryWrapper);
        return R.success("查询成功", songs);
    }

    @Override
    public R selectSongListBySingerId(int singerId) {
        // 设置查询条件
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("singer_id", singerId);
        List<Song> lstSongs = songMapper.selectList(queryWrapper);
        return R.success("查询成功", lstSongs);
    }

    @Override
    public R allSongs() {
        List<Song> songs = songMapper.selectList(null);
        return R.success("查询成功", songs);
    }

    @Override
    public R querySongsByName(String name) {
        //设置查询条件
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Song> songs = songMapper.selectList(queryWrapper);
        return R.success("查询成功",songs);
    }


    @Override
    public R isPay(Integer songId, Integer userId) {
        String msg = "查询歌曲支付状态时发生错误";
        try {
            QueryWrapper<PayItem> itemqw = new QueryWrapper<>();
            itemqw.eq("user_id", userId).eq("song_id", songId);
            List<PayItem> payItems = payItemMapper.selectList(itemqw);
            if (payItems == null) {
                return R.error(msg);
            }
            //歌曲未购买
            if (payItems.isEmpty()) {
                return R.warning("该歌曲还未支付");
            }

            //不等于0时，判断支付清单的状态
            QueryWrapper<Pay> payqw = new QueryWrapper<>();
            payqw.eq("trade_no", payItems.get(0).getTradeNo());
            List<Pay> pays = payMapper.selectList(payqw);

            if (pays == null || pays.size() < 1) {
                return R.error(msg);
            }

            if (pays.get(0).getPayStatus().equals(Constants.PAY_STATUS_1)) {
                return R.success("", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error(msg);
        }
        return R.fatal(msg);
    }

}
